package com.jt.www.job.woxuebao;

import com.alibaba.fastjson.JSON;
import com.jt.www.admin.order_wxb.service.BatchInsService;
import com.jt.www.dao.mapper.woxuebao.InsBatchEntityMapper;
import com.jt.www.domain.enums.common.IsOrNotEnum;
import com.jt.www.enums.woxuebao.ConFirmStatusEnum;
import com.jt.www.model.pay.ResBindDetailQo;
import com.jt.www.model.pay.ResBindQo;
import com.jt.www.model.reps.GenericListResponse;
import com.jt.www.model.reps.GenericResponse;
import com.jt.www.model.woxuebao.InsBatchEntity;
import com.jt.www.remote.ins.InsClient;
import com.jt.www.util.JsonUtils;
import com.jt.www.util.RedisUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 我学保批量投保：检查上传延期支付证明且修改状态的任务
 *
 * @author admin_ltf
 * @create 2019-06-30 09:43
 */
@Component
@EnableScheduling
public class CheckBatchInsBindResourceTask implements SchedulingConfigurer {

    public static final Logger log = LoggerFactory.getLogger(CheckBatchInsBindResourceTask.class);
    /**
     * 单个服务器出单查询缓存失效时间 因不同的定时任务决定
     */
    private static final long IME_OUT = 300L;
    /**
     * 我学保批量投保 绑定资源
     */
    private static final String BATCH_INS_BIND_LOCK_KEY = "EDU_BATCH_INS_BIND";
    @Autowired
    BatchInsService batchInsService;
    @Autowired
    private InsBatchEntityMapper insBatchEntityMapper;
    @Autowired
    private InsClient insClient;
    /**
     * 任务开关
     */
    @Value("${jtpf.batchins.open}")
    private int checkOpen;
    /**
     * 任务执行周期
     */
    @Value("${jtpf.batchins.cron}")
    private String cron;// 每隔10分钟执行一次

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        if (checkOpen != 1) {
            return;
        }
        Runnable task0 = () -> checkStart();
        Trigger trigger0 = triggerContext -> new CronTrigger(cron).nextExecutionTime(triggerContext);
        taskRegistrar.addTriggerTask(task0, trigger0);
    }

    private void checkStart() {
        //多个服务同时启动定时任务，在执行体开始时判断是否有其他线程在执行
        //设置缓存失败表示有在执行的任务
        if (!RedisUtil.setLock(IME_OUT, BATCH_INS_BIND_LOCK_KEY)) {
            return;
        }
        try {
            log.info("我学保批量投保(绑定资源, 修改投保单状态)任务开始!");
            process();
        } catch (Exception e) {
            log.info("我学保批量投保(绑定资源, 修改投保单状态)任务出错!");
        } finally {
            //删除缓存
            RedisUtil.delete(BATCH_INS_BIND_LOCK_KEY);
            log.info("我学保批量投保(绑定资源, 修改投保单状态)任务结束!");
        }
    }

    private void process() {
        // 查询到账确认成功的进行资源绑定
        Weekend<InsBatchEntity> weekend = Weekend.of(InsBatchEntity.class);
        WeekendCriteria<InsBatchEntity, Object> criteria = weekend.weekendCriteria();
        criteria.andEqualTo(InsBatchEntity::getConfirmStatus, ConFirmStatusEnum.SUCCESS.getCode());
        criteria.andEqualTo(InsBatchEntity::getIsDeleted, IsOrNotEnum.NO.getValue());
        //重试5次
        criteria.andLessThan(InsBatchEntity::getTimes, 6);
        //从第0位开始查询，查询出前300个
        RowBounds rowBounds = new RowBounds(0, 300);
        List<InsBatchEntity> resultList = insBatchEntityMapper.selectByExampleAndRowBounds(weekend, rowBounds);
        if (CollectionUtils.isNotEmpty(resultList)) {
            for (InsBatchEntity entity : resultList) {
                String findCode = "BATCH_INS_" + entity.getTbNo();
                // 绑定资源 上传支付证明
                String tbNo = entity.getTbNo();
                ResBindQo resBindQo = new ResBindQo();
                resBindQo.setApplicationFormCode(tbNo);
                List<ResBindDetailQo> detailList = new ArrayList<>();
                ResBindDetailQo resBindDetailQo = new ResBindDetailQo();
                resBindDetailQo.setBizCodeType("02");//02：投保单
                resBindDetailQo.setResCode("P003");
                String resId = entity.getResId();
                if (StringUtils.isEmpty(resId)) {
                    resId = "0";
                }
                resBindDetailQo.setResId(resId);
                resBindDetailQo.setResName(entity.getResName());
                detailList.add(resBindDetailQo);
                resBindQo.setDetailList(detailList);
                try {
                    log.info("线下批单,{} 绑定资源, 修改投保单状态", findCode);
                    ResponseEntity<GenericListResponse<Map<String, String>>> responseResponse = insClient.bindResources(resBindQo);
                    log.info("线下批单,{} 绑定资源, 修改投保单状态回参:{}", findCode, JsonUtils.toJson(responseResponse));

                    if (GenericResponse.CODE_OK.equals(responseResponse.getBody().getCode())) {
                        batchInsService.updateConfirmStatusById(entity.getId(), ConFirmStatusEnum.BIND_SUCCESS, 0);
                    } else {
                        //不修改绑定状态
                        batchInsService.updateConfirmStatusById(entity.getId(), ConFirmStatusEnum.SUCCESS, entity.getTimes() + 1);
                    }
                } catch (Exception e) {
                    log.error("线下批单,{} 绑定资源, 修改投保单状态异常，入参：{}", findCode, JSON.toJSONString(resBindQo), e);
                    batchInsService.updateConfirmStatusById(entity.getId(), ConFirmStatusEnum.SUCCESS, entity.getTimes() + 1);
                }
            }
        }
    }

}
